package com.schoolCommunity.controller;


import com.schoolCommunity.entity.Admin;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.entity.Member;
import com.schoolCommunity.service.AdminService;
import com.schoolCommunity.vo.voSearch;
import com.schoolCommunity.vo.voToken;
import com.schoolCommunity.vo.voUser;
import com.schoolCommunity.vo.voUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YTH
 * @since 2020-09-21
 */
@RestController
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService AdminService;

    //hello
    @GetMapping("/hello")
    public String hello() {
        return "hello_7150";
    }


    //登出
    @PostMapping("/user/logout")
    public Information logout(@RequestHeader("X-Token") String token) {
        Information information = new Information();
        //这里应该有接口验证token的合法和有效性
        //获取token的用户名
        //移除token的登录状态
        information.setMessage("退出成功").setCode(20000).setData("退出成功");
        return information;
    }


    //userInfo
    @GetMapping("/user/info")
    public Information info(@RequestParam("token") String token) {

        //验证token的合法性和有效性
        //ok则返回需要的信息
        //否则返回500
        Information information = new Information();

        try {
            voUserInfo voUserInfo = new voUserInfo();
            voUserInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            voUserInfo.setIntroduction("I am a super administrator");
            voUserInfo.setName("管理者");
            List<String> roles = Arrays.asList("admin");
            voUserInfo.setRoles(roles);

            information.setData(voUserInfo);
            information.setCode(20000);
            information.setMessage("通过");
        } catch (Exception e) {
            information.setData(null);
            information.setCode(500);
            information.setMessage("不通过");
            e.printStackTrace();
        }


        return information;
    }

    //测试后台登录
    @PostMapping("/user/login")
    public Information login(@RequestBody voUser voUser) {
        System.out.println("进入了方法");
        System.out.println(voUser);

        Information information = AdminService.adminLogin(voUser.getUsername(), voUser.getPassword());


        //应该有个方法判断登录成功与否，如果成功则传回对象，失败则传回空
        //这里忽略，直接使用传来的原生数据
        try {
            if (information.getData() != null) {
                String token = "admin-token";  //写死模拟token
                information.setCode(20000).setMessage("登录成功").setData(new voToken(token));
            } else {
                information.setMessage("登录失败").setCode(500).setData("fail");
            }
        } catch (Exception e) {
            information.setMessage("登录失败" + e.getMessage()).setCode(500).setData("fail");
            e.printStackTrace();
        }


        return information;
    }


    //管理员登录
    @PostMapping("/adminLogin/{email}/{password}")
    public Information adminLogin(@PathVariable("email") String email, @PathVariable("password") String password) {
        System.out.println("邮箱：" + email);
        System.out.println("密码：" + password);
        return AdminService.adminLogin(email, password);
    }

    //管理员添加管理员
    @PostMapping("/insertAdmin")
    public Information insertAdmin(@RequestBody Admin admin) throws IOException {
        System.out.println(admin);
        return AdminService.insertAdmin(admin);
    }

    //管理员获取普通用户最大分页数
    @PostMapping("/getPagingMax")
    public Information getPagingMax() {
        return AdminService.getPagingMax();
    }

    //管理员分页获取普通用户数据
    @GetMapping("/pagingGetAllMember/{count}")
    public Information pagingGetAllMember(@PathVariable("count") long count) {
        return AdminService.pagingGetAllMember(count);
    }

    //获取所有管理员信息
    @PostMapping("/getAllAdmin/{count}")
    public Information getAllAdmin(@PathVariable("count") long count) {
        return AdminService.getAllAdmin(count);
    }

    //获取管理员最大分页
    @PostMapping("/getAdminPagingMax")
    public Information getAdminPagingMax() {
        return AdminService.getAdminPagingMax();
    }

    //删除管理员
    @PostMapping("/deleteAdmin/{deleteId}")
    public Information deleteAdmin(@PathVariable("deleteId") long deleteId) throws IOException {
        return AdminService.deleteAdmin(deleteId);
    }

    //搜索用户，传入昵称，姓氏，页数
    @PostMapping("/searchMember/{pageNo}")
    public Information searchMember(@PathVariable("pageNo") int pageNo,
                                    @RequestBody voSearch voSearch
    ) throws IOException {
        System.out.println(voSearch);
        return AdminService.searchMember(voSearch.getNickname(), voSearch.getFamilyName(), pageNo);

    }


}

